package com.vf.cloud.paas.app.master.api.actor.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.vf.cloud.paas.app.master.api.actor.service.IActorClassify;
import com.vf.cloud.paas.app.master.common.domain.ActorClassify;
import com.vf.cloud.paas.common.datasource.annotation.Tx;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.UuidUtil;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/actor/classify")
public class ActorClassifyController {

	@Autowired
	private IActorClassify actorClassifyImpl;

	@RequestMapping(value = "/lazyTree", method = RequestMethod.POST)
	public R<List<ActorClassify>> tree(@RequestBody ActorClassify param) {
		return R.ok(actorClassifyImpl.getChildrens(param));
	}

	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<ActorClassify> save(@RequestBody ActorClassify actorClassify) {
		if (actorClassify == null) {
			return R.failed("参数必填.");
		}

		if (StrKit.isBlank(actorClassify.getId())) {
			actorClassify.setId(UuidUtil.getUUID());
			if (actorClassify.save())
				return R.ok(actorClassify);
		} else {
			if (actorClassify.update())
				return R.ok(actorClassify);
		}
		return R.failed();
	}

	@Tx
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public R<String> delete(@RequestBody ActorClassify actorClassify) {
		try {
			if (actorClassify == null || StrKit.isBlank(actorClassify.getId())) {
				return R.failed("参数必填.");
			}
			actorClassifyImpl.delete(actorClassify.getId());
			return R.ok();
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

}
